package com.google.code.sip.ucenter.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.code.sip.ucenter.model.ResetPasswordEvent;
import com.google.code.sip.ucenter.model.User;
import com.google.code.sip.ucenter.service.ResetPasswordEventManager;
import com.google.code.sip.ucenter.service.UserManager;

@Controller
public class ResetPasswordController {

	@Autowired
	private ResetPasswordEventManager resetPasswordEventManager;

	@Autowired
	private UserManager userManager;

	@RequestMapping("/resetPassword.do")
	public void resetPassword(HttpServletRequest request,
			Map<String, Object> model) {
		String msg = null;
		String username = request.getParameter("uid");
		String password = request.getParameter("password");
		// String time = request.getParameter("time");
		ResetPasswordEvent resetEvent = resetPasswordEventManager.get(username,
				password);
		User user = userManager.getUserByName(username);
		if (user == null || resetEvent == null) {
			msg = "用户找不到，密码无法激活，请联系管理员！";
		} else if (resetEvent.isActive() == false) {
			resetEvent.setActive(true);
			resetPasswordEventManager.save(resetEvent);
			userManager.setPassword(user.getId(), resetEvent.getPassword());
			msg = "密码已激活，现在您可以使用新密码登陆了！";
		}
		model.put("msg", msg);

	}
}
